home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GDEVMEM1.C < prev    next >
C/C++ Source or Header  |  1993-05-26  |  18KB  |  560 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevmem1.c */
  20. /* Generic and monobit "memory" (stored bitmap) device */
  21. /* for Ghostscript library. */
  22. #include "memory_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gxdevice.h"
  26. #include "gxdevmem.h"            /* semi-public definitions */
  27. #include "gdevmem.h"            /* private definitions */
  28.  
  29. /* Define masks for little-endian operation. */
  30. const ushort gdev_mem_swapped_left_masks[17] = {
  31.     0xffff, 0xff7f, 0xff3f, 0xff1f, 0xff0f, 0xff07, 0xff03, 0xff01,
  32.     0xff00, 0x7f00, 0x3f00, 0x1f00, 0x0f00, 0x0700, 0x0300, 0x0100,
  33.     0x0000
  34. };
  35.  
  36. /* ------ Generic code ------ */
  37.  
  38. /* Return the appropriate memory device for a given */
  39. /* number of bits per pixel (0 if none suitable). */
  40. const gx_device_memory *
  41. gdev_mem_device_for_bits(int bits_per_pixel)
  42. {    switch ( bits_per_pixel )
  43.        {
  44.     case 1: return &mem_mono_device;
  45.     case 2: return &mem_mapped2_color_device;
  46.     case 4: return &mem_mapped4_color_device;
  47.     case 8: return &mem_mapped8_color_device;
  48.     case 16: return &mem_true16_color_device;
  49.     case 24: return &mem_true24_color_device;
  50.     case 32: return &mem_true32_color_device;
  51.     default: return 0;
  52.        }
  53. }
  54.  
  55. /* Compute the size of the bitmap storage, */
  56. /* including the space for the scan line pointer table. */
  57. /* Note that scan lines are padded to a multiple of 4 bytes, */
  58. /* and additional padding may be needed if the pointer table */
  59. /* must be aligned 0 mod 8. */
  60. private ulong
  61. mem_bitmap_bits_size(const gx_device_memory *dev)
  62. {    return round_up((ulong)dev->height * gdev_mem_raster(dev),
  63.             max(4, arch_align_long_mod));
  64. }
  65. ulong
  66. gdev_mem_bitmap_size(const gx_device_memory *dev)
  67. {        return mem_bitmap_bits_size(dev) +
  68.         (ulong)dev->height * sizeof(byte *);
  69. }
  70.  
  71. /* Open a memory device, allocating the data area if appropriate, */
  72. /* and create the scan line table. */
  73. int
  74. mem_open(gx_device *dev)
  75. {    byte *scan_line;
  76.     uint raster = mdev->raster = gdev_mem_raster(mdev);
  77.     byte **pptr;
  78.     byte **pend;
  79.     if ( mdev->memory_procs != 0 )
  80.     {    /* Allocate the data now. */
  81.         ulong size = gdev_mem_bitmap_size(mdev);
  82.         if ( (uint)size != size )
  83.             return gs_error_limitcheck;
  84.         mdev->base = (byte *)(*mdev->memory_procs->alloc)(1, (uint)size, "mem_open");
  85.         if ( mdev->base == 0 )
  86.             return gs_error_VMerror;
  87.     }
  88.     scan_line = mdev->base;
  89.         pptr = (byte **)byte_ptr_add(scan_line, mem_bitmap_bits_size(mdev));
  90.     pend = pptr + dev->height;
  91.     mdev->line_ptrs = pptr;
  92.     while ( pptr < pend )
  93.        {    *pptr++ = scan_line;
  94.         scan_line = byte_ptr_add(scan_line, raster);
  95.        }
  96.     return 0;
  97. }
  98.  
  99. /* Return the initial transformation matrix */
  100. void
  101. mem_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  102. {    pmat->xx = mdev->initial_matrix.xx;
  103.     pmat->xy = mdev->initial_matrix.xy;
  104.     pmat->yx = mdev->initial_matrix.yx;
  105.     pmat->yy = mdev->initial_matrix.yy;
  106.     pmat->tx = mdev->initial_matrix.tx;
  107.     pmat->ty = mdev->initial_matrix.ty;
  108. }
  109.  
  110. /* Test whether a device is a memory device */
  111. int
  112. gs_device_is_memory(const gx_device *dev)
  113. {    /* We can't just compare the procs, or even an individual proc, */
  114.     /* because we might be tracing.  Compare the device name, */
  115.     /* and hope for the best. */
  116.     const char *name = dev->dname;
  117.     int i;
  118.     for ( i = 0; i < 6; i++ )
  119.       if ( name[i] != "image("[i] ) return 0;
  120.     return 1;
  121. }
  122.  
  123. /* Ensure that the data bytes are in big-endian order. */
  124. /* This is no longer needed. */
  125. void
  126. gdev_mem_ensure_byte_order(gx_device_memory *dev)
  127. {
  128. }
  129.  
  130. /* Close a memory device, freeing the data area if appropriate. */
  131. int
  132. mem_close(gx_device *dev)
  133. {    if ( mdev->memory_procs != 0 )
  134.       (*mdev->memory_procs->free)((char *)mdev->base,
  135.         1, (uint)gdev_mem_bitmap_size(mdev), "mem_close");
  136.     return 0;
  137. }
  138.  
  139. /* Copy a scan line to a client. */
  140. #undef chunk
  141. #define chunk byte
  142. int
  143. mem_get_bits(gx_device *dev, int y, byte *str, byte **actual_data)
  144. {    byte *src;
  145.     if ( y < 0 || y >= dev->height )
  146.         return gs_error_rangecheck;
  147.     src = scan_line_base(mdev, y);
  148.     if ( actual_data == 0 )
  149.         memcpy(str, src, gx_device_raster(dev, 0));
  150.     else
  151.         *actual_data = src;
  152.     return 0;
  153. }
  154.  
  155. /* Return the xfont procedure vector. */
  156. gx_xfont_procs *
  157. mem_get_xfont_procs(gx_device *dev)
  158. {    gx_device *target = mdev->target;
  159.     return (target == 0 ? gx_default_get_xfont_procs(dev) :
  160.         (*target->procs->get_xfont_procs)(target));
  161. }
  162.  
  163. /* Return the xfont device. */
  164. gx_device *
  165. mem_get_xfont_device(gx_device *dev)
  166. {    gx_device *target = mdev->target;
  167.     return (target == 0 ? gx_default_get_xfont_device(dev) :
  168.         (*target->procs->get_xfont_device)(target));
  169. }
  170.  
  171. /* ------ Monochrome ------ */
  172.  
  173. /* Procedures */
  174. private dev_proc_copy_mono(mem_mono_copy_mono);
  175. private dev_proc_fill_rectangle(mem_mono_fill_rectangle);
  176.  
  177. /* The device descriptor. */
  178. private gx_device_procs mem_mono_procs =
  179.   mem_procs(gx_default_map_rgb_color, gx_default_map_color_rgb,
  180.     mem_mono_copy_mono, gx_default_copy_color, mem_mono_fill_rectangle);
  181.  
  182. /* The instance is public. */
  183. const gx_device_memory mem_mono_device =
  184.   mem_device("image(mono)", 1, mem_mono_procs);
  185.  
  186. /* Convert x coordinate to byte offset in scan line. */
  187. #define x_to_byte(x) ((x) >> 3)
  188.  
  189. /* Fill a rectangle with a color. */
  190. #undef chunk
  191. #define chunk mono_chunk
  192. private int
  193. mem_mono_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  194.   gx_color_index color)
  195. {    uint bit;
  196.     chunk right_mask;
  197.     byte fill;
  198.     declare_scan_ptr(dest);
  199.     fit_fill(dev, x, y, w, h);
  200.     setup_rect(dest);
  201. #define write_loop(stat)\
  202.  { int line_count = h;\
  203.    chunk *ptr = dest;\
  204.    do { stat; inc_chunk_ptr(ptr, draster); }\
  205.    while ( --line_count );\
  206.  }
  207. #define write_partial(msk)\
  208.    if ( fill ) write_loop(*ptr |= msk)\
  209.    else write_loop(*ptr &= ~msk)
  210.     switch ( color )
  211.        {
  212.     case 0: fill = mdev->invert; break;
  213.     case 1: fill = ~mdev->invert; break;
  214.     case gx_no_color_index: return 0;        /* transparent */
  215.     default: return -1;        /* invalid */
  216.        }
  217.     bit = x & chunk_align_bit_mask;
  218.     if ( bit + w <= chunk_bits )
  219.        {    /*
  220.          * Only one word. We have to split following statement
  221.          * because of a bug in the Xenix C compiler (it produces
  222.          * a signed rather than an unsigned shift if we don't
  223.          * split).
  224.          */
  225.         set_mono_thin_mask(right_mask, w, bit);
  226.        }
  227.     else
  228.        {    int byte_count;
  229.         if ( bit )
  230.            {    chunk mask;
  231.             set_mono_left_mask(mask, bit);
  232.             write_partial(mask);
  233.             dest++;
  234.             w += bit - chunk_bits;
  235.            }
  236.         set_mono_right_mask(right_mask, w & chunk_bit_mask);
  237.         if ( (byte_count = (w >> 3) & -chunk_bytes) != 0 )
  238.            {    write_loop(memset(ptr, fill, byte_count));
  239.             inc_chunk_ptr(dest, byte_count);
  240.            }
  241.        }
  242.     if ( right_mask )
  243.         write_partial(right_mask);
  244.     return 0;
  245. }
  246.  
  247. /* Copy a monochrome bitmap. */
  248.  
  249. /* Fetch a chunk from the source. */
  250. /* The source data are always stored big-endian. */
  251. /* Note that the macros always cast cptr, */
  252. /* so it doesn't matter what the type of cptr is. */
  253. /* cshift = chunk_bits - shift. */
  254. #undef chunk
  255. #if arch_is_big_endian
  256. #  define chunk uint
  257. #  define cfetch_right(cptr, shift, cshift)\
  258.     (cfetch_aligned(cptr) >> shift)
  259. #  define cfetch_left(cptr, shift, cshift)\
  260.     (cfetch_aligned(cptr) << shift)
  261. /* Fetch a chunk that straddles a chunk boundary. */
  262. #  define cfetch2(cptr, cskew, skew)\
  263.     (cfetch_left(cptr, cskew, sk